package controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pojo.Department;
import service.DepartmentService;
import java.util.List;

@Api(value = "科室管理", description = "医院科室相关接口")
@RestController
@CrossOrigin(origins = "*")
@RequestMapping("/department")
public class DepartmentController {

    @Autowired
    private DepartmentService departmentService;

    @ApiOperation(value = "查询所有科室", notes = "获取医院所有科室信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回科室列表", response = Department.class, responseContainer = "List")
    })
    @GetMapping("/findAll")
    public ResponseEntity<List<Department>> findAll() {
        return ResponseEntity.ok(departmentService.findAll());
    }

    @ApiOperation(value = "新增科室", notes = "创建一个新的科室信息")
    @ApiResponses({
            @ApiResponse(code = 201, message = "科室创建成功"),
            @ApiResponse(code = 400, message = "无效的科室数据")
    })
    @PostMapping("/addDepartment")
    public ResponseEntity<Void> addDepartment(@RequestBody Department department) {
        departmentService.addDepartment(department);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @ApiOperation(value = "更新科室信息", notes = "根据科室ID更新科室信息")
    @ApiResponses({
            @ApiResponse(code = 200, message = "科室信息更新成功"),
            @ApiResponse(code = 404, message = "未找到对应科室")
    })
    @PostMapping("/updateDepartment")
    public ResponseEntity<Void> updateDepartment(@RequestBody Department department) {
        int success = departmentService.updateDepartment(department);
        return success > 0 ?
                ResponseEntity.ok().build() :
                ResponseEntity.notFound().build();
    }
    @ApiOperation(value = "删除科室", notes = "根据科室ID删除科室")
    @ApiResponses({
            @ApiResponse(code = 204, message = "科室删除成功"),
            @ApiResponse(code = 404, message = "未找到对应科室")
    })
    @DeleteMapping("/deleteDepartment/{department_id}")
    public ResponseEntity<Void> deleteDepartment(@PathVariable("department_id") int department_id) {
        int success = departmentService.deleteDepartment(department_id);
        return success > 0 ?
                ResponseEntity.noContent().build() :
                ResponseEntity.notFound().build();
    }


    @ApiOperation(value = "查询指定级别的科室", notes = "根据级别查询科室")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功返回科室列表", response = Department.class, responseContainer = "List")
    })
    @GetMapping("/findByLeve")
    public ResponseEntity<List<Department>> findByLeve(@RequestParam int leve) {
        return ResponseEntity.ok(departmentService.findByLeve(leve));
    }
}